﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace WindowsFormsApplication1
{
    class VolumeSensor : Sensor
    {
        private EndpointVolume e;
        private Timer myTimer;
        private const int TIMER_INTERVAL = 5000;
        public VolumeSensor()
        {
            name = "Audio Volume Sensor";
            try
            {
                e = new EndpointVolume();
                data = (new EndpointVolume()).MasterVolume;
                myTimer = new Timer(); //Initialize Timer
                myTimer.Elapsed += new ElapsedEventHandler(VolumeEventArrived);
                myTimer.Interval = TIMER_INTERVAL;
                myTimer.Start();
            }
            catch (Exception f)
            {
            }
        }
        public void VolumeEventArrived(object source, ElapsedEventArgs f)
        {
            if (!e.MasterVolume.Equals((float)data))//if volume changed
            {
                data = e.MasterVolume;
                Notify();
            }
        }
    }
}
